package com.wangce.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.wangce.entity.Dim;
import com.wangce.entity.Order;
import com.wangce.entity.User;
import com.wangce.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * ClassName: OrderController
 * Description:
 * date: 2021/11/22 9:20
 *
 * @author 凡人
 * @since JDK 1.8
 */
@Controller
public class OrderController {

    @Autowired
    OrderService service;

    @RequestMapping("index")
    public String index(){
        return "index";
    }

    /**
     * 列表
     * @param model
     * @param d
     * @param cur
     * @param size
     * @return
     */
    @RequestMapping("list")
    public String list(Model model,Dim d, @RequestParam(defaultValue = "1") Integer cur,
                       @RequestParam(defaultValue = "3") Integer size){
        PageInfo<Order> o = service.list(d,cur,size);
        model.addAttribute("list",o);
        model.addAttribute("d",d);
        return "orderList";
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        long l = IdUtil.getSnowflake(31, 31).nextId();
        model.addAttribute("l",l);
        return "add";
    }

    @RequestMapping("add")
    public String add(Order o, HttpSession session){
        User user = (User) session.getAttribute("user");
        o.setUser(user);
        Integer i = service.add(o);
        if(i>0){
            return "redirect:list";
        }
        return "add";
    }
}
